﻿
/****** Object:  User Defined Function dbo.FiscalWeekStart    Script Date: 10/14/2009 10:39:06 PM ******/

CREATE function FiscalWeekStart( @FiscalWeek int, @FiscalYear int ) 
returns smalldatetime
as 
begin
declare @FirstDayOfYear int
declare @tmpDate datetime
 
-- Get the first day of the year ( based on Monday rather than Sunday )
set @FirstDayOfYear = dbo.FirstDayOfYear(@FiscalYear) - 1
if @FirstDayOfYear < 1  set @FirstDayOfYear = 7


set @tmpDate = convert( datetime, '1/1/' + cast( @FiscalYear as char ) , 101)

if @FiscalWeek > 1 
   set @tmpDate = dateadd( day, ( ( @FiscalWeek - 1 ) * 7 ) - @FirstDayOfYear + 1, @tmpDate )

return( @tmpDate )
end

